home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / tri.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  9KB  |  404 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30. #define    SOLID 1
  31. #define    LINE 2
  32. #define    POINT 3
  33.  
  34. GLenum rgb, doubleBuffer, directRender, windType;
  35. GLint windW, windH;
  36.  
  37. GLenum dithering = GL_TRUE;
  38. GLenum showVerticies = GL_TRUE;
  39. GLenum hideBottomTriangle = GL_FALSE;
  40. GLenum outline = GL_TRUE;
  41. GLenum culling = GL_FALSE;
  42. GLenum winding = GL_FALSE;
  43. GLenum face = GL_FALSE;
  44. GLenum state = SOLID;
  45. GLenum aaMode = GL_FALSE;
  46. GLenum shade = GL_TRUE;
  47.  
  48. GLint color1, color2, color3;
  49.  
  50. float zRotation = 90.0;
  51. float zoom = 1.0;
  52.  
  53. float boxA[3] =
  54. {-100, -100, 0};
  55. float boxB[3] =
  56. {100, -100, 0};
  57. float boxC[3] =
  58. {100, 100, 0};
  59. float boxD[3] =
  60. {-100, 100, 0};
  61.  
  62. float p0[3] =
  63. {-125, -80, 0};
  64. float p1[3] =
  65. {-125, 80, 0};
  66. float p2[3] =
  67. {172, 0, 0};
  68.  
  69. static void Init(void)
  70. {
  71.   float r, g, b;
  72.   float percent1, percent2;
  73.   GLint i, j;
  74.  
  75.   glClearColor(0.0, 0.0, 0.0, 0.0);
  76.  
  77.   glLineStipple(1, 0xF0F0);
  78.  
  79.   glEnable(GL_SCISSOR_TEST);
  80.  
  81.   if (!rgb) {
  82.     for (j = 0; j <= 12; j++) {
  83.       if (j <= 6) {
  84.     percent1 = j / 6.0;
  85.     r = 1.0 - 0.8 * percent1;
  86.     g = 0.2 + 0.8 * percent1;
  87.     b = 0.2;
  88.       }
  89.       else {
  90.     percent1 = (j - 6) / 6.0;
  91.     r = 0.2;
  92.     g = 1.0 - 0.8 * percent1;
  93.     b = 0.2 + 0.8 * percent1;
  94.       }
  95.       tkSetOneColor(j + 18, r, g, b);
  96.       for (i = 0; i < 16; i++) {
  97.     percent2 = i / 15.0;
  98.     tkSetOneColor(j * 16 + 1 + 32, r * percent2, g * percent2, b * percent2);
  99.       }
  100.     }
  101.     color1 = 18;
  102.     color2 = 24;
  103.     color3 = 30;
  104.   }
  105. }
  106.  
  107. static void Reshape(int width, int height)
  108. {
  109.  
  110.   windW = (GLint) width;
  111.   windH = (GLint) height;
  112. }
  113.  
  114. static GLenum Key(int key, GLenum mask)
  115. {
  116.  
  117.   switch (key) {
  118.     case TK_ESCAPE:
  119.       tkQuit();
  120.     case TK_LEFT:
  121.       zRotation += 0.5;
  122.       break;
  123.     case TK_RIGHT:
  124.       zRotation -= 0.5;
  125.       break;
  126.     case TK_Z:
  127.       zoom *= 0.75;
  128.       break;
  129.     case TK_z:
  130.       zoom /= 0.75;
  131.       if (zoom > 10) {
  132.     zoom = 10;
  133.       }
  134.       break;
  135.     case TK_1:
  136.       glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  137.       break;
  138.     case TK_2:
  139.       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  140.       break;
  141.     case TK_3:
  142.       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  143.       break;
  144.     case TK_4:
  145.       state = POINT;
  146.       break;
  147.     case TK_5:
  148.       state = LINE;
  149.       break;
  150.     case TK_6:
  151.       state = SOLID;
  152.       break;
  153.     case TK_7:
  154.       culling = !culling;
  155.       break;
  156.     case TK_8:
  157.       winding = !winding;
  158.       break;
  159.     case TK_9:
  160.       face = !face;
  161.       break;
  162.     case TK_v:
  163.       showVerticies = !showVerticies;
  164.       break;
  165.     case TK_s:
  166.       shade = !shade;
  167.       (shade) ? glShadeModel(GL_SMOOTH) : glShadeModel(GL_FLAT);
  168.       break;
  169.     case TK_h:
  170.       hideBottomTriangle = !hideBottomTriangle;
  171.       break;
  172.     case TK_o:
  173.       outline = !outline;
  174.       break;
  175.     case TK_m:
  176.       dithering = !dithering;
  177.       break;
  178.     case TK_0:
  179.       aaMode = !aaMode;
  180.       if (aaMode) {
  181.     glEnable(GL_POLYGON_SMOOTH);
  182.     glEnable(GL_BLEND);
  183.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  184.     if (!rgb) {
  185.       color1 = 32;
  186.       color2 = 128;
  187.       color3 = 224;
  188.     }
  189.       }
  190.       else {
  191.     glDisable(GL_POLYGON_SMOOTH);
  192.     glDisable(GL_BLEND);
  193.     if (!rgb) {
  194.       color1 = 18;
  195.       color2 = 24;
  196.       color3 = 30;
  197.     }
  198.       }
  199.       break;
  200.     default:
  201.       return GL_FALSE;
  202.   }
  203.   return GL_TRUE;
  204. }
  205.  
  206. static void BeginPrim(void)
  207. {
  208.  
  209.   switch (state) {
  210.     case SOLID:
  211.       glBegin(GL_POLYGON);
  212.       break;
  213.     case LINE:
  214.       glBegin(GL_LINE_LOOP);
  215.       break;
  216.     case POINT:
  217.       glBegin(GL_POINTS);
  218.       break;
  219.     default:
  220.       break;
  221.   }
  222. }
  223.  
  224. static void EndPrim(void)
  225. {
  226.  
  227.   glEnd();
  228. }
  229.  
  230. static void Draw(void)
  231. {
  232.   float scaleX, scaleY;
  233.  
  234.   glViewport(0, 0, windW, windH);
  235.  
  236.   glMatrixMode(GL_PROJECTION);
  237.   glLoadIdentity();
  238.   gluOrtho2D(-175, 175, -175, 175);
  239.   glMatrixMode(GL_MODELVIEW);
  240.  
  241.   glScissor(0, 0, windW, windH);
  242.  
  243.   (culling) ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
  244.   (winding) ? glFrontFace(GL_CCW) : glFrontFace(GL_CW);
  245.   (face) ? glCullFace(GL_FRONT) : glCullFace(GL_BACK);
  246.  
  247.   (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  248.  
  249.   glClear(GL_COLOR_BUFFER_BIT);
  250.  
  251.   TK_SETCOLOR(windType, TK_GREEN);
  252.   glBegin(GL_LINE_LOOP);
  253.   glVertex3fv(boxA);
  254.   glVertex3fv(boxB);
  255.   glVertex3fv(boxC);
  256.   glVertex3fv(boxD);
  257.   glEnd();
  258.  
  259.   if (!hideBottomTriangle) {
  260.     glPushMatrix();
  261.  
  262.     glScalef(zoom, zoom, zoom);
  263.     glRotatef(zRotation, 0, 0, 1);
  264.  
  265.     TK_SETCOLOR(windType, TK_BLUE);
  266.     BeginPrim();
  267.     glVertex3fv(p0);
  268.     glVertex3fv(p1);
  269.     glVertex3fv(p2);
  270.     EndPrim();
  271.  
  272.     if (showVerticies) {
  273.       (rgb) ? glColor3fv(tkRGBMap[TK_RED]) : glIndexf(color1);
  274.       glRectf(p0[0] - 2, p0[1] - 2, p0[0] + 2, p0[1] + 2);
  275.       (rgb) ? glColor3fv(tkRGBMap[TK_GREEN]) : glIndexf(color2);
  276.       glRectf(p1[0] - 2, p1[1] - 2, p1[0] + 2, p1[1] + 2);
  277.       (rgb) ? glColor3fv(tkRGBMap[TK_BLUE]) : glIndexf(color3);
  278.       glRectf(p2[0] - 2, p2[1] - 2, p2[0] + 2, p2[1] + 2);
  279.     }
  280.  
  281.     glPopMatrix();
  282.   }
  283.  
  284.   scaleX = (float)(windW - 20) / 2 / 175 * (175 - 100) + 10;
  285.   scaleY = (float)(windH - 20) / 2 / 175 * (175 - 100) + 10;
  286.  
  287.   glViewport(scaleX, scaleY, windW - 2 * scaleX, windH - 2 * scaleY);
  288.  
  289.   glMatrixMode(GL_PROJECTION);
  290.   glLoadIdentity();
  291.   gluOrtho2D(-100, 100, -100, 100);
  292.   glMatrixMode(GL_MODELVIEW);
  293.  
  294.   glScissor(scaleX, scaleY, windW - 2 * scaleX, windH - 2 * scaleY);
  295.  
  296.   glPushMatrix();
  297.  
  298.   glScalef(zoom, zoom, zoom);
  299.   glRotatef(zRotation, 0, 0, 1);
  300.  
  301.   glPointSize(10);
  302.   glLineWidth(5);
  303.   glEnable(GL_POINT_SMOOTH);
  304.   glEnable(GL_LINE_STIPPLE);
  305.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  306.  
  307.   TK_SETCOLOR(windType, TK_RED);
  308.   BeginPrim();
  309.   (rgb) ? glColor3fv(tkRGBMap[TK_RED]) : glIndexf(color1);
  310.   glVertex3fv(p0);
  311.   (rgb) ? glColor3fv(tkRGBMap[TK_GREEN]) : glIndexf(color2);
  312.   glVertex3fv(p1);
  313.   (rgb) ? glColor3fv(tkRGBMap[TK_BLUE]) : glIndexf(color3);
  314.   glVertex3fv(p2);
  315.   EndPrim();
  316.  
  317.   glPointSize(1);
  318.   glLineWidth(1);
  319.   glDisable(GL_POINT_SMOOTH);
  320.   glDisable(GL_LINE_STIPPLE);
  321.   glBlendFunc(GL_ONE, GL_ZERO);
  322.  
  323.   if (outline) {
  324.     TK_SETCOLOR(windType, TK_WHITE);
  325.     glBegin(GL_LINE_LOOP);
  326.     glVertex3fv(p0);
  327.     glVertex3fv(p1);
  328.     glVertex3fv(p2);
  329.     glEnd();
  330.   }
  331.  
  332.   glPopMatrix();
  333.  
  334.   glFlush();
  335.  
  336.   if (doubleBuffer) {
  337.     tkSwapBuffers();
  338.   }
  339. }
  340.  
  341. static GLenum Args(int argc, char **argv)
  342. {
  343.   GLint i;
  344.  
  345.   rgb = GL_TRUE;
  346.   doubleBuffer = GL_FALSE;
  347.   directRender = GL_TRUE;
  348.  
  349.   for (i = 1; i < argc; i++) {
  350.     if (strcmp(argv[i], "-ci") == 0) {
  351.       rgb = GL_FALSE;
  352.     }
  353.     else if (strcmp(argv[i], "-rgb") == 0) {
  354.       rgb = GL_TRUE;
  355.     }
  356.     else if (strcmp(argv[i], "-sb") == 0) {
  357.       doubleBuffer = GL_FALSE;
  358.     }
  359.     else if (strcmp(argv[i], "-db") == 0) {
  360.       doubleBuffer = GL_TRUE;
  361.     }
  362.     else if (strcmp(argv[i], "-dr") == 0) {
  363.       directRender = GL_TRUE;
  364.     }
  365.     else if (strcmp(argv[i], "-ir") == 0) {
  366.       directRender = GL_FALSE;
  367.     }
  368.     else {
  369.       printf("%s (Bad option).\n", argv[i]);
  370.       return GL_FALSE;
  371.     }
  372.   }
  373.   return GL_TRUE;
  374. }
  375.  
  376. void main(int argc, char **argv)
  377. {
  378.  
  379.   if (Args(argc, argv) == GL_FALSE) {
  380.     tkQuit();
  381.   }
  382.  
  383.   windW = 600;
  384.   windH = 300;
  385.   tkInitPosition(0, 0, windW, windH);
  386.  
  387.   windType = (rgb) ? TK_RGB : TK_INDEX;
  388.   windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  389.   windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  390.   tkInitDisplayMode(windType);
  391.  
  392.   if (tkInitWindow("Triangle Test") == GL_FALSE) {
  393.     tkQuit();
  394.   }
  395.  
  396.   Init();
  397.  
  398.   tkExposeFunc(Reshape);
  399.   tkReshapeFunc(Reshape);
  400.   tkKeyDownFunc(Key);
  401.   tkDisplayFunc(Draw);
  402.   tkExec();
  403. }
  404.